package com.cycleit.applicationexample.command{
	import com.cycleit.applicationexample.model.ConfigModel;
	import com.cycleit.cycleframework.event.CycleEvent;
	import com.cycleit.cycleframework.interfaces.ICycleCommand;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	
	public class LoadConfigCommand implements ICycleCommand{
		
		private var _modelo:ConfigModel = ConfigModel.getInstance();
		
		private var _config:URLLoader;
		
		public function execute(e:CycleEvent):void{ 
			// We load our configuration file config.xml
			_config = new URLLoader();
			_config.addEventListener(Event.COMPLETE, readConfig);
			_config.load(new URLRequest("config.xml?" + ((Math.random()*50000)|0)));
		}
		
		private function readConfig(event: Event): void {
			var configXML:XML = new XML(event.target.data);
			
			// We initialize our config model
			_modelo.title = configXML.title;
			// Following lines show you how to configure an AMFChannel and fictitious server urls
			_modelo.urlServer = configXML.urlServicesServer;
			_modelo.urlAMF = configXML.endpointAMF;
			_modelo.urlResources = configXML.resourcesURL;
			
			// We create an AMFChannel
			var channel:AMFChannel = new AMFChannel('amf-channel', _modelo.urlAMF);
			_modelo.channelSet = new ChannelSet(); 
			_modelo.channelSet.addChannel(channel);
		}
	}
}